home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 September (IDG) / Sep99.iso / Shareware World / Utilities / Text Processing / Alpha / Tcl / Modes / latex Mode / latexUtilities.tcl < prev    next >
Encoding:
Text File  |  1999-04-12  |  5.5 KB  |  171 lines  |  [TEXT/ALFA]

  1. #############################################################################
  2. #############################################################################
  3. #
  4. # latexUtilities.tcl (sourced on demand)
  5. #
  6. #############################################################################
  7. #
  8. # Author:  Tom Scavo <trscavo@syr.edu>, Vince Darley
  9. #
  10. #############################################################################
  11. #############################################################################
  12.  
  13. # Delete all unnecessary comments from the current document:
  14. proc deleteComments {} {
  15.     if {![dialog::yesno "Warning!  This operation can not be undone.  \
  16.       Continue anyway?"]} { return }
  17.  
  18.     catch {
  19.     replaceString {}
  20.     performSearch -f 1 -r 1 -i 1 -m 0 -- "^\[ \t\]*%\[^\r\n\]*\r" [minPos]
  21.     replaceAll
  22.     }
  23.  
  24.     catch {
  25.     replaceString {}
  26.     performSearch -f 1 -r 1 -i 1 -m 0 -- "\[ \t\]+%\[^\r\n\]*" [minPos]
  27.     replaceAll
  28.     }
  29.  
  30.     catch {
  31.     replaceString {\1%}
  32.     performSearch -f 1 -r 1 -i 1 -m 0 -- "(\[^\\\](\\\\)*)%\[^\r\n\]*" [minPos]
  33.     replaceAll
  34.     }
  35. }
  36.  
  37. # Converts all straight quotes to their TeX equivalents.
  38. proc convertQuotes {} {
  39.     global searchNoisily
  40.     message "working…"
  41.     watchCursor
  42.     set messageString "selection"
  43.     if {[pos::compare [set start [getPos]] == [set end [selEnd]]]} {
  44.     set messageString "document"
  45.     set start [minPos]
  46.     set end [maxPos]
  47.     }
  48.     set text [getText $start $end]
  49.     # Convert all left double quotes:
  50.     set convert1 [regsub -all "\(\^\|\[\ \r\t\(\[\{\]\)\"" $text {\1``} text]
  51.     # Convert all right double quotes:
  52.     set convert2 [regsub -all "\(\[\^\\\\\]\)\"" $text {\1''} text]
  53.     # Convert all left single quotes:
  54.     set convert3 [regsub -all "\(\^\|\[\ \r\t\(\[\{\]\)\'" $text {\1`} text]
  55.     if {$convert1 || $convert2 || $convert3} {
  56.     replaceText $start $end $text
  57.     message "all quotes in $messageString converted"
  58.     } else {
  59.     if {$searchNoisily} {beep}
  60.     message "no quotes found in $messageString"
  61.     }
  62. }
  63.  
  64. # Convert all dollar signs to their LaTeX equivalents:
  65. proc convertDollarSigns {} {
  66.     global searchNoisily
  67.     if {[isSelection]} {
  68.     set messageString "selection"
  69.     } else {
  70.     set messageString "document"
  71.     }
  72.     set subs2 [convertDoubleDollarSigns]
  73.     if {$subs2 == -1} {
  74.     beep
  75.     alertnote "unmatched double dollar signs in $messageString"
  76.     } else {
  77.     set subs1 [convertSingleDollarSigns]
  78.     if {$subs1 == -1} {
  79.         beep
  80.         alertnote "unmatched single dollar sign in $messageString"
  81.     } elseif {$subs1 == 0 && $subs2 == 0} {
  82.         if {$searchNoisily} {beep}
  83.         message "no dollar signs found in $messageString"
  84.     } else {
  85.         message "$subs1 pairs of \$…\$ and $subs2 pairs of \$\$…\$\$ removed from $messageString"
  86.     }
  87.     }
  88. }
  89.  
  90. # Converts all $$...$$ pairs to \[...\] and returns the number of such 
  91. # pairs converted.  If the dollar signs are unbalanced, does nothing 
  92. # and returns -1.
  93. proc convertDoubleDollarSigns {} {
  94.     watchCursor
  95.     set messageString "selection"
  96.     if {[pos::compare [set start [getPos]] == [set end [selEnd]]]} {
  97.     set messageString "document"
  98.     set start [minPos]
  99.     set end [maxPos]
  100.     }
  101.     set text [getText $start $end]
  102.     set subs [regsub -all {(^|[^\\])\$\$([^$]*)\$\$} $text {\1\\[\2\\]} text]
  103.     if {[containsDoubleDollarSigns $text]} {return -1}
  104.     if {$subs} {
  105.     replaceText $start $end $text
  106.     }
  107.     return [expr $subs]
  108. }
  109.  
  110. # Returns true if the argument contains non-literal double dollar
  111. # signs, and false otherwise.
  112. proc containsDoubleDollarSigns {text} {
  113.     return [regexp {(^|[^\\])\$\$} $text]
  114. }
  115.  
  116. # Converts all $...$ pairs to \(...\), maintains the cursor position, 
  117. # and returns the number of such pairs converted.  If the dollar signs 
  118. # are unbalanced, does nothing and returns -1.
  119. proc convertSingleDollarSigns {} {
  120.     watchCursor
  121.     set subs1 0; set subs2 0; set subs3 0
  122.     set pos [getPos]; set pos2 $pos
  123.     if {[pos::compare [set start $pos] == [set end [selEnd]]]} {
  124.     set isSelection 0
  125.     set start [minPos]
  126.     set end [maxPos]
  127.     set text1 [getText $start $pos]
  128.     set subs1 [regsub -all {(^|[^\\])\$([^$]*)\$} $text1 {\1\\(\2\\)} text1]
  129.     # Is there a dollar sign left over?  If so, search backward for this
  130.     # dollar sign and prepare to do a substitution on the text to the right
  131.     # of this dollar sign.
  132.     if {[containsSingleDollarSign $text1]} {
  133.         set searchString {[^\\]\$}
  134.         set searchResult [search -s -n -f 0 -m 0 -i 1 -r 1 $searchString [expr $pos-1]]
  135.         set pos2 [lindex $searchResult 0]
  136.         set text1 [string range $text1 0 [expr $pos2 + (2 * $subs1)]]
  137.         set pos [expr $pos + 2]
  138.     }
  139.     set text2 [getText $pos2 $end]
  140.     set subs2 [regsub -all {(^|[^\\])\$([^$]*)\$} $text2 {\1\\(\2\\)} text2]
  141.     # Is there a dollar sign left over?  If so, it's unbalanced.
  142.     if {[containsSingleDollarSign $text2]} {return -1}
  143.     append text $text1 $text2
  144.     } else {
  145.     set isSelection 1
  146.     set text [getText $start $end]
  147.     set subs3 [regsub -all {(^|[^\\])\$([^$]*)\$} $text {\1\\(\2\\)} text]
  148.     # Is there a dollar sign left over?  If so, it's unbalanced.
  149.     if {[containsSingleDollarSign $text]} {return -1}
  150.     }
  151.     if {$subs1 || $subs2 || $subs3} {
  152.     replaceText $start $end $text
  153.     # If there is a selection, just put it back.  Otherwise, adjust the
  154.     # cursor position based on the number of substitutions.
  155.     if {$isSelection} {
  156.         set end [getPos]
  157.         select $start $end
  158.     } else {
  159.         goto [pos::math $pos + [expr {2 * $subs1}]]
  160.     }
  161.     }
  162.     return [expr $subs1 + $subs2 + $subs3]
  163. }
  164.  
  165. # Returns true if the argument contains a non-literal dollar sign,
  166. # and false otherwise.
  167. proc containsSingleDollarSign {text} {
  168.     return [regexp {(^|[^\\])\$} $text]
  169. }
  170.  
  171.